<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>
        保修添加
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/css/main.css" media="all">
    <script type="text/javascript" src="/lib/loading/okLoading.js"></script>
    <!-- 引入ElementUI样式 -->
    <link rel="stylesheet" href="/css/index.css">
</head>
<body>
<div id="app">
    <div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>服务管理</cite></a>
              <a><cite>{{title}}</cite></a>
            </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
        <el-form :model="repair" :rules="rules" ref="repairFormRef" label-width="120px" class="demo-ruleForm">
            <el-form-item label="所属小区" prop="communityName">
                <el-select v-model="communityVo" placeholder="请选择所属小区" value-key="id">
                    <el-option
                            v-for="community in communityList"
                            :key="community.id"
                            :label="community.name"
                            :value="community">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="所属楼栋" prop="buildingName">
                <el-select v-model="buildingVo" placeholder="请选择所属栋"  value-key="id">
                    <el-option
                            v-for="building in buildingList"
                            :key="building.id"
                            :label="building.name"
                            :value="building">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="保修人员" prop="personalName">
                <el-select v-model="personalVo" placeholder="请选择所属保修人员" value-key="id">
                    <el-option
                            v-for="personal in personalList"
                            :key="personal.id"
                            :label="personal.name"
                            :value="personal">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="设备名称" prop="deviceName">
                <el-select v-model="deviceVo" placeholder="请选择所属设备" value-key="id">
                    <el-option
                            v-for="device in deviceList"
                            :key="device.id"
                            :label="device.name"
                            :value="device">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio-group v-model="repair.status">
                    <el-radio label="0">待受理</el-radio>
                    <el-radio label="1">已受理</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="保修描述" prop="description">
                <el-input v-model="repair.description" type="textarea">
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="save()" style="text-align: center">保存</el-button>
                <el-button @click="clearAll()" style="text-align: center">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</div>
<script src="lib/layui/layui.js" charset="utf-8"></script>
<script src="js/x-layui.js" charset="utf-8"></script>
<!--导入Vue依赖包-->
<script src="js/vue.js" charset="utf-8"></script>
<!-- 引入Element组件库 -->
<script src="js/index.js"></script>
<!--导入Axios依赖包-->
<script src="js/axios.js" charset="utf-8"></script>
<script type="text/javascript">
    var app = new Vue({
        el:"#app",
        data:{
            title:"添加报修",
            repair:{id:""},
            communityVo:{},
            communityList:{},
            buildingVo:{},
            buildingList:{},
            personalVo:{},
            personalList:{},
            deviceVo:{},
            deviceList:{},
            pickerOptions: {
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
            rules: {
                communityName: [
                    { required: true, message: '请选择小区名称', trigger: 'blur' }
                ],
                buildingName: [
                    { required: true, message: '请选择楼栋名称', trigger: 'blur' }
                ],
                personalName: [
                    { required: true, message: '请选择报修人员', trigger: 'blur' }
                ],
                deviceName: [
                    { required: true, message: '请选择设备名称', trigger: 'blur' }
                ],
                status: [
                    { required: true, message: '请选择状态', trigger: 'change' }
                ],
                description: [
                    { required: true, message: '请输入保修描述', trigger: 'blur' }
                ]
            }
        },
        methods:{
            findById(){
                axios.get("/repair/findById?id="+this.repair.id).then((res)=>{
                    this.repair = res.data.data;
                });
            },
            findCommunity(){
                axios.get("/community/find").then((res)=>{
                    this.communityList = res.data.data;
                });
            },
            findBuilding(){
                axios.get("/building/find").then((res)=>{
                    this.buildingList = res.data.data;
                });
            },
            findPersonal(){
                axios.get("/personal/find").then((res)=>{
                    this.personalList = res.data.data;
                });
            },
            findDevice(){
                axios.get("/device/find").then((res)=>{
                    this.deviceList = res.data.data;
                });
            },
            //表达重置
            clearAll() {
                this.$refs.repairFormRef.resetFields();
            },
            save(){
                var method = "";
                if(this.repair.id != null){
                    method = "update"
                }else{
                    method = "add"
                }
                this.repair.communityName=this.communityVo.name;
                this.repair.communityId=this.communityVo.id;
                this.repair.buildingName=this.buildingVo.name;
                this.repair.buildingId=this.buildingVo.id;
                this.repair.ownerName=this.personalVo.name;
                this.repair.ownerId=this.personalVo.id;
                this.repair.deviceName=this.deviceVo.name;
                this.repair.deviceId=this.deviceVo.id;
                axios.post("/repair/"+method,this.repair).then((res)=>{
                    if(res.data.flag){
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                });
            },
            getQueryString(name){
                let reg = `(^|&)${name}=([^&]*)(&|$)`
                let r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]); return null;
            }
        },
        created:function(){
            layui.use(['element','layer','form'], function(){
                $ = layui.jquery;//jquery
                lement = layui.element();//面包导航
                layer = layui.layer;//弹出层
                form = layui.form();
                okLoading.close($);
            });
            //获取父页面传递参数，小区ID
            this.repair.id = this.getQueryString("id");
            //根据小区ID获取小区对象，进行数据回显
            if(this.repair.id != null && this.repair.id !== ''){
                this.title="修改车辆";
                this.findById();
            }
            this.findCommunity();
            this.findPersonal();
            this.findBuilding();
            this.findDevice();
        }
    });
</script>
</body>
</html>